package com.superbug.moi.stupiccqupt.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.balysv.materialmenu.MaterialMenuDrawable;
import com.balysv.materialmenu.extras.toolbar.MaterialMenuIconToolbar;
import com.superbug.moi.stupiccqupt.R;
import com.superbug.moi.stupiccqupt.ui.adapter.DrawerListAdapter;
import com.superbug.moi.stupiccqupt.ui.fragment.GuessPasswordFragment;
import com.superbug.moi.stupiccqupt.ui.fragment.SearchIdFragment;
import com.superbug.moi.stupiccqupt.ui.fragment.SearchPicFragment;
import com.superbug.moi.stupiccqupt.ui.widget.IpListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity implements View.OnClickListener,AdapterView.OnItemClickListener {

    private MaterialMenuIconToolbar materialMenu;
    private TextView tab1;
    private TextView tab2;
    private TextView tab3;
    private View color;
    private ViewPager mViewPager;
    protected static View mView1;
    protected static View mView2;
    protected static View mView3;
    protected List<View> viewList;
    private List<Fragment> fragList;
    private MyFragmentPagerAdapter adapter;
    private DrawerLayout mDrawerLayout;
    private IpListView mListView;
    private DrawerListAdapter listAdapter;
    private List<String> items = new ArrayList<String>();
    private boolean isDrawerOpened;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setToolbar();
        init();
    }

    private void init() {

        tab1 = (TextView) findViewById(R.id.tv_1);
        tab1.setOnClickListener(this);
        tab2 = (TextView) findViewById(R.id.tv_2);
        tab2.setOnClickListener(this);
        tab3 = (TextView) findViewById(R.id.tv_3);
        tab3.setOnClickListener(this);

        tab1.setTextColor(getResources().getColor(R.color.white));
        tab2.setTextColor(getResources().getColor(R.color.light_white));
        tab3.setTextColor(getResources().getColor(R.color.light_white));

        color = findViewById(R.id.color);

        int width = getScreenWidth(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((width / 3)-60, 8);
        color.setLayoutParams(params);

        mViewPager = (ViewPager) findViewById(R.id.view_pager);

        LayoutInflater lf = getLayoutInflater().from(this);
        mView1 = lf.inflate(R.layout.fragment_search_pic, null);
        mView2 = lf.inflate(R.layout.fragment_search_id, null);
        mView3 = lf.inflate(R.layout.fragment_guess_password, null);

        viewList = new ArrayList<View>();
        viewList.add(mView1);
        viewList.add(mView2);
        viewList.add(mView3);

        fragList = new ArrayList<Fragment>();
        fragList.add(new SearchPicFragment());
        fragList.add(new SearchIdFragment());
        fragList.add(new GuessPasswordFragment());

        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList);
        mViewPager.setAdapter(adapter);

        mViewPager.setOnPageChangeListener(new InternalViewPagerListener());

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mListView = (IpListView) findViewById(R.id.lv_drawer);
        items.add("记录学号");
        items.add("查询记录");
        items.add("关于应用");
        items.add("退出程序");
        listAdapter = new DrawerListAdapter(this, R.layout.item_drawer_list, items);
        mListView.setAdapter(listAdapter);
        mListView.setOnItemClickListener(this);
    }

    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }


    private void setToolbar() {
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("  " + getResources().getString(R.string.app_name));
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                // Handle your drawable state here
                switch (materialMenu.getDrawable().getIconState()) {
                    case BURGER:
                        mDrawerLayout.openDrawer(GravityCompat.START);
                        break;
                    case ARROW:
                        mDrawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case X:
                        break;
                    case CHECK:
                        break;
                }

            }
        });
        materialMenu = new MaterialMenuIconToolbar(this, Color.WHITE, MaterialMenuDrawable.Stroke.THIN) {
            @Override
            public int getToolbarViewId() {
                return R.id.toolbar;
            }
        }; // or retrieve from your custom view, etc
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                materialMenu.setTransformationOffset(
                        MaterialMenuDrawable.AnimationState.BURGER_ARROW,
                        isDrawerOpened ? 2 - slideOffset : slideOffset
                );
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                isDrawerOpened = true;
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                isDrawerOpened = false;
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                if(newState == DrawerLayout.STATE_IDLE) {
                    if(isDrawerOpened) materialMenu.setState(MaterialMenuDrawable.IconState.ARROW);
                    else materialMenu.setState(MaterialMenuDrawable.IconState.BURGER);
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                Toast.makeText(this, "不想敲了！", Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(this, "不想敲了！", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(this, "不想敲了！\n数据来源于教务在线\n仅供娱乐！\n切记请不要用此伤害他人！" +
                                "\n如有任何问题请联系我！\nsuper8moi@gmail.com",
                        Toast.LENGTH_SHORT).show();
                break;
            case 3:
                finish();
                break;
        }
    }


    private class InternalViewPagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            color.setX(((position + positionOffset) * getScreenWidth(getApplicationContext()) / 3)+30);
        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    tab1.setTextColor(getResources().getColor(R.color.white));
                    tab2.setTextColor(getResources().getColor(R.color.light_white));
                    tab3.setTextColor(getResources().getColor(R.color.light_white));
                    break;
                case 1:
                    tab1.setTextColor(getResources().getColor(R.color.light_white));
                    tab2.setTextColor(getResources().getColor(R.color.white));
                    tab3.setTextColor(getResources().getColor(R.color.light_white));
                    break;
                case 2:
                    tab1.setTextColor(getResources().getColor(R.color.light_white));
                    tab2.setTextColor(getResources().getColor(R.color.light_white));
                    tab3.setTextColor(getResources().getColor(R.color.white));
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragList;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragList) {
            super(fm);
            this.fragList = fragList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragList.get(position);
        }

        @Override
        public int getCount() {
            return fragList.size();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement


        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_1:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.tv_2:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.tv_3:
                mViewPager.setCurrentItem(2);
                break;
        }
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        isDrawerOpened = mDrawerLayout.isDrawerOpen(Gravity.START); // or END, LEFT, RIGHT
        materialMenu.syncState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        materialMenu.onSaveInstanceState(outState);
    }
}
